<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺状态" prop="shopStatus">
        <el-select v-model="queryParams.shopStatus" placeholder="请选择店铺状态" clearable size="small">
          <el-option
            v-for="dict in shopStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="法人名称" prop="personName">
        <el-input
          v-model="queryParams.personName"
          placeholder="请输入法人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['amz:shop:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['amz:shop:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['amz:shop:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['amz:shop:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="店铺代码" align="center" prop="shopCode"/>
      <el-table-column label="店铺名称" align="center" prop="shopName"/>
      <el-table-column label="店铺状态" width="80" align="center" prop="shopStatus" :formatter="shopStatusFormat"/>
      <el-table-column label="法人名称" width="80" align="center" prop="personName"/>
      <el-table-column label="公司名称" align="center" prop="companyName" :show-overflow-tooltip="true"/>
      <el-table-column label="服务器远程ip" align="center" prop="serverIp"/>
      <el-table-column label="创建人" width="80" align="center" prop="createBy"/>
      <el-table-column label="更新时间" width="120" align="center" prop="updateTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['amz:shop:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['amz:shop:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改店铺维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-col :span="12">
          <el-form-item label="店铺代码" prop="shopCode">
            <el-input v-model="form.shopCode" disabled placeholder="新增时会自动生成，无需用户填写"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="店铺名称" prop="shopName">
            <el-input v-model="form.shopName" :disabled="form.shopCode!=null" placeholder="请输入店铺名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="店铺状态" prop="shopStatus">
            <el-select v-model="form.shopStatus" placeholder="请选择店铺状态" :style="{width: '100%'}">
              <el-option
                v-for="dict in shopStatusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="法人名称" prop="personName">
            <el-input v-model="form.personName" placeholder="请输入法人名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份证号码" prop="personIdcradNo">
            <el-input v-model="form.personIdcradNo" placeholder="请输入身份证号码"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份证地址" prop="personIdcardAddress">
            <el-input v-model="form.personIdcardAddress" placeholder="请输入身份证地址"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电话号码" prop="personPhone">
            <el-input v-model="form.personPhone" placeholder="请输入电话号码"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="信用卡号" prop="personCreditCardNo">
            <el-input v-model="form.personCreditCardNo" placeholder="请输入信用卡号"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电子邮件" prop="personEmail">
            <el-input v-model="form.personEmail" placeholder="请输入电子邮件"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="统一社会信用代码" prop="companyCode">
            <el-input v-model="form.companyCode" placeholder="请输入统一社会信用代码"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公司名称" prop="companyName">
            <el-input v-model="form.companyName" placeholder="请输入公司名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公司注册地址" prop="companyRegisteredAddress">
            <el-input v-model="form.companyRegisteredAddress" placeholder="请输入内容"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公司经营地址" prop="companyBusinessAddress">
            <el-input v-model="form.companyBusinessAddress" placeholder="请输入内容"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公司行政区划" prop="companyAdministrativeDistricts">
            <el-input v-model="form.companyAdministrativeDistricts" placeholder="请输入公司行政区划"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="服务器远程ip" prop="serverIp">
            <el-input v-model="form.serverIp" placeholder="请输入服务器远程ip"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listShop, getShop, delShop, addShop, updateShop, exportShop} from "@/api/amz/shop";

  export default {
    name: "Shop",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 店铺维护表格数据
        shopList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,

        // 店铺状态字典
        shopStatusOptions: [],

        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          shopStatus: null,
          personName: null,
          companyName: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          shopName: [
            {required: true, message: "店铺名称不能为空", trigger: "blur"}
          ],
          shopStatus: [
            {required: true, message: "店铺状态不能为空", trigger: "change"}
          ],
          personPhone: [
            {required: true, message: "电话号码不能为空", trigger: "change"}
          ],
          personEmail: [
            {required: true, message: "电子邮件不能为空", trigger: "change"}
          ],
          personIdcradNo: [
            {required: true, message: "身份证号码不能为空", trigger: "change"}
          ],
          personName: [
            {required: true, message: "法人名称不能为空", trigger: "change"}
          ],
        }
      };
    },
    created() {
      this.getList();

      this.getDicts("amz_status").then(response => {
        this.shopStatusOptions = response.data;
      });

    },
    methods: {
      /** 查询店铺维护列表 */
      getList() {
        this.loading = true;
        listShop(this.queryParams).then(response => {
          this.shopList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 店铺状态字典翻译
      shopStatusFormat(row, column) {
        return this.selectDictLabel(this.shopStatusOptions, row.shopStatus);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          shopId: null,
          shopCode: null,
          shopName: null,
          shopStatus: null,
          personName: null,
          personIdcradNo: null,
          personIdcardAddress: null,
          personPhone: null,
          personCreditCardNo: null,
          personEmail: null,
          companyCode: null,
          companyName: null,
          companyRegisteredAddress: null,
          companyBusinessAddress: null,
          companyAdministrativeDistricts: null,
          serverIp: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.shopId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加店铺维护";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const shopId = row.shopId || this.ids
        getShop(shopId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改店铺维护";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.shopId != null) {
              updateShop(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addShop(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const shopIds = row.shopId || this.ids;
        this.$confirm('该操作将同时删除授权相关数据！是否确认删除店铺?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delShop(shopIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有店铺维护数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportShop(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
